
from feature_data.const_data import *
from feature_data.XmlData2DictData import Xml2Dict

from feature_data.Dict2Venue_Graphfeature import Feature_venue_extration
from feature_data.answer_extration import Answer_extration

from nd_utils.Graph_Cluster_util import *
from nd_utils.networkx_util import *


class Graph_venue_stage():
    def __init__(self, feature_venue_extration, answer_extration):

        self.feature_venue_extration = feature_venue_extration

        self.init_graph = self.feature_venue_extration.init_graph
        self.cluster_covenue_pair_set_list = self.feature_venue_extration.cluster_covenue_pair_set_list

        self.third_cluster_graph = self._get_cluster_graph()
        self.true_cluster_num = answer_extration.true_cluster_num
        self.true_paperidx_cluster_list = answer_extration.true_paperidx_cluster_list

        self.simulation_occur_flag = self._get_simulation_occur_flag()

        self.subgraph_list = self._get_subgraph_list()
        self.third_subgraph_nodes_list = self._get_third_subgraph_nodes_list()

    def _get_cluster_graph(self):
        pair_set_list = self.cluster_covenue_pair_set_list
        cluster_graph = add_pair_set_to_graph(pair_set_list, deepcopy_graph(self.init_graph),_weight=1.5 )
        return cluster_graph

    def _get_simulation_occur_flag(self):
        predict_cluster_num = get_subgraph_num(self.third_cluster_graph)
        if predict_cluster_num >= self.true_cluster_num:
            return True
        else:
            return False

    def _get_subgraph_list(self):
        # if self.simulation_occur_flag:
        if True:
            return get_subgraphs(self.third_cluster_graph)
        else:
            return get_subgraphs(self.init_graph)

    def _get_third_subgraph_nodes_list(self):
        return get_subgraph_nodes_list(self.subgraph_list)

def print_clust_total(cluster_list):
    paper_pair_set_list = cluster_list
    sum = 0

    union_set = []

    set_length_list = []
    for paper_pair_set in paper_pair_set_list:
        # print list(paper_pair_set), len(paper_pair_set)
        print sorted(list(paper_pair_set)), len(paper_pair_set)
        set_length_list.append(len(paper_pair_set))
        sum += len(paper_pair_set)

        union_set.extend(list(paper_pair_set))
    print "sum: ", sum
    # print sorted(list(union_set)), len(list(union_set))
    print sorted(list(set(union_set))) , len(set(union_set))
    print set_length_list

from nd_utils.trans_clusteridxlist_paperidx_pairset import get_paperidx_pairset_list
from model_metric import Model_metric

if __name__ == '__main__':
    for xml_file_path in xml_filepath_list:
        xml2dict = Xml2Dict(xml_file_path)
        feature_venue_extration = Feature_venue_extration(xml2dict)
        answer_extration = Answer_extration(xml2dict)
        third_stage_clust = Graph_venue_stage(feature_venue_extration, answer_extration)

        # print first_stage_clust.cluster_list
        # print_clust_total(third_stage_clust.subgraph_list)

        print third_stage_clust.simulation_occur_flag

        # print_subgraphs(first_stage_clust)
        paperid_pairset_list = get_paperidx_pairset_list(third_stage_clust.true_paperidx_cluster_list)

        predict_paperid_pairset_list = get_paperidx_pairset_list(third_stage_clust.third_subgraph_nodes_list)

        model_metric = Model_metric(paperid_pairset_list, predict_paperid_pairset_list )

        print model_metric.pairwise_precision, model_metric.pairwise_recall, model_metric.pairwise_f1

"""
False
0.258435582822 0.742290748899 0.383390216155
True
0.656171284635 0.560215053763 0.604408352668
True
1.0 0.961636828645 0.980443285528
False
0.086052955665 0.93634840871 0.157620188919
False
0.538647342995 0.817848410758 0.649514563107
True
0.309921581998 0.769686706181 0.441905687895
False
0.266974291365 0.9902200489 0.420560747664
False
0.569424964937 0.764595103578 0.652733118971
True
0.995433789954 0.986425339367 0.990909090909
True
0.827517447657 0.930493273543 0.87598944591
"""